package com.lyx.question.controller;

import com.lyx.common.result.R;
import com.lyx.common.utils.JwtUtils;
import com.lyx.question.dto.Indexchapter;
import com.lyx.question.dto.ThreeChapterInfo;
import com.lyx.question.entity.OneSubject;
import com.lyx.question.service.SubjectService;
import com.lyx.question.vo.ChildrenVo;
import com.lyx.question.vo.OneSubjectVo;
import com.lyx.question.vo.SubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 章节
 * @author 14667
 * @date 2022/7/20 10:28
 */
@RestController()
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 后台管理-树形展示章节分类
     * @return
     */
    @GetMapping("/listWithTree")
    public R listWithTree(){
        List<OneSubject> oneSubjects= subjectService.listWithTree();
        return R.ok().data("data",oneSubjects);
    }

    /**
     * 新建一级章节
     * @param subjectVo
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@Validated @RequestBody OneSubjectVo subjectVo){
        boolean flag = subjectService.addChapter(subjectVo);
        if(flag){
            return R.ok().message("新建章节成功");
        }
        return R.error().message("新建章节失败");
    }

    /**
     * 添加子章节
     * @param childrenVo
     * @return
     */
    @PostMapping("/addChildChapter")
    public R addChildChapter(@Validated @RequestBody ChildrenVo childrenVo){
        Boolean flag = subjectService.addChildChapter(childrenVo);
        if(flag){
            return R.ok().message("添加子类成功");
        }
        return R.error().message("添加子类失败");
    }

    /**
     * 删除章节
     * @param id
     * @return
     */
    @PutMapping("/deleteChapter")
    public R deleteChapter(@Valid @NotNull(message = "章节id不能为空") @Min(value = 0,message = "章节id应该是整形") @RequestParam Integer id){
        boolean flag = subjectService.deleteChapter(id);
        if(flag){
            return R.ok().message("删除章节成功");
        }
        return R.error().message("删除章节失败");
    }

    /**
     * 首页题目分级展示
     * @return
     */
    @GetMapping("/index")
    public R indexChapter(){

        List<Indexchapter> data = subjectService.indexChapter();
        return R.ok().data("data",data);
    }

    /**
     * 前端展示按钮，父类打开，子类不打开，但是其父类打开
     * @param chapterId
     * @param chapterName
     * @return
     */
    @PutMapping("/show")
    public R show(@Valid @NotNull(message = "章节id不能为空") @Min(value = 0,message = "章节id应该是整形") @RequestParam Integer chapterId,
                  @Valid @NotEmpty(message = "章节名字不能为空") @RequestParam String chapterName){
        subjectService.show(chapterId,chapterName);
        return R.ok().message("前端展示打开");
    }

    /**
     * 父类关闭，子类都关闭
     * @param chapterName
     * @param chapterId
     * @return
     */
    @PutMapping("/close")
    public R close(@Valid @NotNull(message = "章节id不能为空") @Min(value = 0,message = "章节id应该是整形") @RequestParam Integer chapterId,
                   @Valid @NotEmpty(message = "章节名字不能为空") @RequestParam String chapterName){
        subjectService.close(chapterName,chapterId);
        return R.ok().message("前端展示关闭");
    }

    @GetMapping("test")
    public R test(){
        return R.ok();
    }

    /**
     * 根据id查询三级分类的信息
     * @param request
     * @param chapterId
     * @return
     */
    @GetMapping("indexThreeChapterInfo")
    public R indexThreeChapterInfo(HttpServletRequest request,Integer chapterId){
        List<ThreeChapterInfo> data = subjectService.indexThreeChapterInfo(request,chapterId);
        return R.ok().data("data",data).message("获取三级章节成功");
    }

}
